#include "cvt_win.h"
#include <Windows.h>
#include <strsafe.h>

unsigned int win_utf8_codepage()
{
	return CP_UTF8;
}

unsigned int win_cvt_default_pagecode()
{
#ifdef _CONVERSION_DONT_USE_THREAD_LOCALE
	return CP_ACP;
#else
	return CP_THREAD_ACP;
#endif
}

unsigned int win_cvt_a2w_size(unsigned int codePage, const char *src)
{
	if(src == NULL) return 1;
	return (unsigned int)::MultiByteToWideChar(codePage, 0, src, -1, NULL, 0);
}

void win_cvt_a2w(unsigned int codePage, const char *src, wchar_t *dst, unsigned int destsize)
{
	::MultiByteToWideChar(codePage, 0, src, -1, dst, destsize);
}


unsigned int win_cvt_w2a_size(unsigned int codePage, const wchar_t *src)
{
	if(src == NULL) return 1;
	return (unsigned int)::WideCharToMultiByte(codePage, 0, src, -1, NULL, 0, NULL, NULL);
}

void win_cvt_w2a(unsigned int codePage, const wchar_t *src, char *dst, unsigned int destsize)
{
	::WideCharToMultiByte(codePage, 0, src, -1, dst, destsize, NULL, NULL);
}

int win_v_printf_a(char * dest, size_t dest_size, const char *fmt, va_list arg_list)
{
	HRESULT ret = StringCchVPrintfA(dest, dest_size, fmt, arg_list);
	if(ret == STRSAFE_E_INSUFFICIENT_BUFFER)
		return 1;
	if(ret == S_OK)
		return 0;
	return -1;
}

int win_v_printf_w(wchar_t * dest, size_t dest_size, const wchar_t *fmt, va_list arg_list)
{
	HRESULT ret = StringCchVPrintfW(dest, dest_size, fmt, arg_list);
	if(ret == STRSAFE_E_INSUFFICIENT_BUFFER)
		return 1;
	if(ret == S_OK)
		return 0;
	return -1;
}
